package cn.pink.gamesrv.human.module.gm;

import cn.pink.common.proto.Msg;
import cn.pink.common.proto.MsgIds;
import cn.pink.core.support.observer.MsgObserver;
import cn.pink.gamesrv.human.HumanObject;
import cn.pink.gamesrv.msg.MsgParamHuman;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * GM
 * @Author: pink
 * @Date: 2022/8/8 11:23
 */
public class GMHandler {

    @MsgObserver(MsgIds.CSGM)
    static public void onCSGM(MsgParamHuman param) {
        HumanObject humanObj = param.getHumanObj();
        Msg.CSGM msg = param.getMsg();

        List<String> params = msg.getParamsList();

        String cmd = params.get(0);

        List<String> args = new ArrayList<>(params);

        args.remove(0);

        boolean flag = false;
        for (Method method : GMHandler.class.getMethods()) {
            if (method.getName().equalsIgnoreCase(cmd)){
                try {
                    method.invoke(null, humanObj, args);
                } catch (Exception e){
                    e.printStackTrace();
                }
                flag = true;
                break;
            }
        }

        if (!flag){
//            humanObj.sendHint(ErrorResult.DEBUG("未找到命令 " + args[0]));
            //TODO 未找到命令
        }
    }

    static public void testItem(HumanObject humanObj, List<String> args){
        humanObj.getModItem().test(args.get(0));
    }

    static public void createGroup(HumanObject humanObj, List<String> args){
        String name = args.get(0);
        String notice = args.get(1);
        int headId = Integer.parseInt(args.get(2));
        int apply = Integer.parseInt(args.get(3));
        int requireLv = Integer.parseInt(args.get(4));

        humanObj.getModGroup().createGroup(name, notice, headId, apply, requireLv);
    }

    static public void joinGroup(HumanObject humanObj, List<String> args){
        humanObj.getModGroup().joinGroup(Long.parseLong(args.get(0)));
    }
}
